package org.cupk2.tools.db.commons;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class DBRule implements QueryRule {
    private String key;// where key
    private Object value;// where value
    private String compara;// like,} % = > < >= <=
    private List<QueryRule> rules = new ArrayList<QueryRule>();


    public static DBRule getInstance(String key, String value, String comString) {
        DBRule cRule = new DBRule(key, value, comString);
        return cRule;
    }


    public DBRule addRule(String key, String value, String comString) {
        DBRule cRule = new DBRule(key, value, comString);
        rules.add(cRule);
        return this;
    }

    public DBRule(String key, Object value, String compara) {
        compara = compara.replaceAll("[;'\"]", "");
        CupkSqls.wipeVirus(key);
        if (compara.toUpperCase().equals("IN")) {
            CupkSqls.wipeVirus(value.toString().replace(",", "").replace("'", ""));
        } else {
            CupkSqls.wipeVirus(value.toString());
        }
        CupkSqls.wipeVirus(compara);
        this.key = key.trim().toUpperCase();
        this.value = value;
        this.compara = compara.trim().toUpperCase();
        this.rules.add(this);
    }


    public String getSql() {
        StringBuffer where_ = new StringBuffer();
        compara = compara.toUpperCase();
        if (key != null && value != null && compara != null) {
            where_.append("  AND  ");
            where_.append(key.toUpperCase());
            if (compara.indexOf("LIKE") >= 0) {
                where_.append(" ");
                where_.append("LIKE");
            } else {
                where_.append(" ");
                where_.append(compara);
            }
            where_.append(expendVal());
            where_.append(" ");
        }
        return where_.toString();
    }

    /**构造值
     * @return
     */
    private String expendVal() {
        StringBuffer valStr = new StringBuffer();
        if (compara.equals("LIKE")) {
            valStr.append(" '%");
            valStr.append(value.toString());
            valStr.append("%'");
        } else if (compara.equals("-LIKE")) {
            valStr.append(" '%");
            valStr.append(value.toString());
            valStr.append("'");
        } else if (compara.equals("LIKE-")) {
            valStr.append(" '");
            valStr.append(value.toString());
            valStr.append("%'");
        } else if (compara.equals("IS NOT")) {
            valStr.append(" ");
            valStr.append(value.toString());
            valStr.append(" ");
        } else if (compara.equals("IS")) {
            valStr.append(" ");
            valStr.append(value.toString());
            valStr.append(" ");
        } else if (compara.equals("IN")) {
            valStr.append("(");
            valStr.append(value.toString());
            valStr.append(")");
        } else {
            if (value instanceof String) {
                valStr.append(" '");
                valStr.append(value.toString());
                valStr.append("'");
            } else if (value instanceof BigDecimal) {
                valStr.append(" ");
                valStr.append(value.toString());
                valStr.append("");
            } else {
                valStr.append(" ");
                valStr.append(value.toString());
                valStr.append("");
            }
        }
        return valStr.toString();
    }

    public List<QueryRule> getRules() {
        return rules;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public String getCompara() {
        return compara;
    }

    public void setCompara(String compara) {
        this.compara = compara;
    }
}
